{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import torch\n",
    "from BertChineseTextClassificationPytorch.models.bert import Config,Model\n",
    "import torch.nn.functional as F\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from transformers import BertModel,BertTokenizer\n",
    "from transformer_viewer import Glimpse\n",
    "from util import adaptor_embed, adaptor_model, get_label_dict\n",
    "import matplotlib.pyplot as plt \n",
    "from matplotlib import font_manager\n",
    "#为了显示日文字体\n",
    "myfont = font_manager.FontProperties(fname='./IPAexfont00401/ipaexg.ttf')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "if torch.cuda.is_available():\n",
    "    device = torch.device('cuda')\n",
    "else:\n",
    "    device = torch.device('cpu')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练模型保存label文件\n",
    "####可修改#####\n",
    "filep = 'BertChineseTextClassificationPytorch/japanData/label.txt'\n",
    "#预训练模型路径（加载vocab文件）\n",
    "MODEL_PATH_OTHER = './BertChineseTextClassificationPytorch/multi/'\n",
    "#训练模型保存文件\n",
    "####可修改#####\n",
    "MODEL_PATH ='./BertChineseTextClassificationPytorch/saved_dict/bert.ckpt'\n",
    "config = Config(MODEL_PATH_OTHER)\n",
    "# config.bert_path = MODEL_PATH_OTHER\n",
    "label2id_context = get_label_dict()\n",
    "\n",
    "config.num_classes = len(label2id_context)\n",
    "model = Model(config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[38;2;0;0;0mmin \u001b[0m\u001b[38;2;12;0;0m■\u001b[0m\u001b[38;2;25;0;0m■\u001b[0m\u001b[38;2;38;0;0m■\u001b[0m\u001b[38;2;51;0;0m■\u001b[0m\u001b[38;2;63;0;0m■\u001b[0m\u001b[38;2;76;0;0m■\u001b[0m\u001b[38;2;89;0;0m■\u001b[0m\u001b[38;2;102;0;0m■\u001b[0m\u001b[38;2;114;0;0m■\u001b[0m\u001b[38;2;127;0;0m■\u001b[0m\u001b[38;2;140;0;0m■\u001b[0m\u001b[38;2;153;0;0m■\u001b[0m\u001b[38;2;165;0;0m■\u001b[0m\u001b[38;2;178;0;0m■\u001b[0m\u001b[38;2;191;0;0m■\u001b[0m\u001b[38;2;204;0;0m■\u001b[0m\u001b[38;2;216;0;0m■\u001b[0m\u001b[38;2;229;0;0m■\u001b[0m\u001b[38;2;242;0;0m■\u001b[0m\u001b[38;2;255;0;0m■\u001b[0m\u001b[38;2;255;0;0m max\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "tokenizer = BertTokenizer.from_pretrained(MODEL_PATH_OTHER)\n",
    "model.load_state_dict(torch.load(MODEL_PATH,map_location=device))\n",
    "# idx_label_map = {value:key for key, value in label2id_context.items()}\n",
    "\n",
    "viewer = Glimpse(model, tokenizer, adaptor_embed, adaptor_model, device, spliter=' ', id2label=label2id_context)\n",
    "\n",
    "viewer.color_bar()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "html = open('output.html','w')\n",
    "html.write(\"<!DOCtype HTML>\\n<head><title>test结果</title></head>\\n<body>\")\n",
    "def writeHtml(html, t_label, predLabel, score, sens): \n",
    "    html_con = ''\n",
    "    for i in range(len(score)):\n",
    "        if score[i]>0:\n",
    "            html_con += \"<span style=\\\"color:rgb(\"+str(255*score[i]) +\",0,0);\\\">\"+sens[i]+\" \"+\"</span>\"\n",
    "        else:\n",
    "            html_con +=  \"<span style=\\\"color:rgb(,0,0\"+str(255*score[i]) +\");\\\">\"+sens[i]+\" \" +\"</span>\"\n",
    "    html.write('<p>'+sen+'</p>')\n",
    "    html.write('<p> true label : '+t_label+'</p>')\n",
    "    html.write('<p> true label : '+ predLabel +'</p>')\n",
    "    html.write(html_con)\n",
    "    html.write('<p> ==========================================================</p>')\n",
    "    html.write(\"</body>\")\n",
    "    html.flush()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/dfsdata2/dongxz1_data/project/transformer_viewer/util.py:57: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  context.append(torch.tensor(label).to(device, dtype=torch.long))\n",
      "/dfsdata2/dongxz1_data/project/transformer_viewer/util.py:59: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
      "  label =  torch.tensor(label).to(device)\n"
     ]
    }
   ],
   "source": [
    "#测试，下载output.html，并用浏览器打开\n",
    "sen ='it監査サホートリクエストを申請する'\n",
    "label = 'ht__it_audit_support_request'\n",
    "sens =  tokenizer.tokenize(sen)\n",
    "input_id, score, color_text, predLabel = viewer.view(sen,label)\n",
    "writeHtml(html,label,predLabel,score,sens)\n",
    "writeHtml(html,label,predLabel,score,sens)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "#######在这里添加文件，读取，获取文本，因为文件格式不一致，暂时不写了\n",
    "with open(\"test.txt\",'r',encoding='utf-8') as fr:\n",
    "    for line in fr:\n",
    "        #cons = line.split('\\t')\n",
    "        sen = cons[0]\n",
    "        label = cons[1]\n",
    "        sens = tokenizer.tokenize(sen)\n",
    "        input_id, score, color_text, predLabel = viewer.view(sen,label)\n",
    "        writeHtml(html,label,predLabel,score,sens)\n",
    "        \n",
    "###执行完代码之后，下载output.html文件，使用浏览器打开就可以了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
